#include "parcel_dao.h"
#include <QDebug>
#include "sqlite3.h"

ParcelDao::ParcelDao()
{

}

bool ParcelDao::init()
{
    db = dbutil.getDB();

    if (db == NULL) {
        return false;
    }

    return true;
}

QList<Parcel> ParcelDao::findAll()
{
    sqlite3_stmt* stmt = NULL;
    QString sql = "SELECT * FROM parcel;";

    QList<Parcel> results;
    Parcel parcel;

    const char* sqlstr = sql.toLatin1().data();

    int ret = sqlite3_prepare(dbutil.getDB(), sqlstr, sql.size(), &stmt, NULL);
    if (ret != SQLITE_OK) {
        qWarning("Search failed.\n");
        return results;
    }

    while (sqlite3_step(stmt) == SQLITE_ROW) {
        parcel.setExpressNum(QString((const char*)sqlite3_column_text(stmt, 0)));
        parcel.setLogisticsNum(QString((const char*)sqlite3_column_text(stmt, 1)));
        parcel.setBarcode(QString((const char*)sqlite3_column_text(stmt, 2)));
        parcel.setSender(QString((const char*)sqlite3_column_text(stmt, 3)));
        parcel.setRecipient(QString((const char*)sqlite3_column_text(stmt, 4)));
        parcel.setSendingAddr(QString((const char*)sqlite3_column_text(stmt, 5)));
        parcel.setRecivingAddr(QString((const char*)sqlite3_column_text(stmt, 6)));
        parcel.setCurrentAddr(QString((const char*)sqlite3_column_text(stmt, 7)));
        parcel.setNextAddr(QString((const char*)sqlite3_column_text(stmt, 8)));
        parcel.setIsOut(sqlite3_column_int(stmt, 9));

        results.append(parcel);
    }

    sqlite3_finalize(stmt);

    return results;
}

bool ParcelDao::findByBarcode(const QString* barcode, Parcel* parcel)
{
    sqlite3_stmt* stmt = NULL;
    QString sql = QString("SELECT * FROM parcel WHERE barcode='%1'").arg(*barcode);

    int ret = sqlite3_prepare(dbutil.getDB(), sql.toLatin1().data(), sql.size(), &stmt, NULL);
    if (ret != SQLITE_OK) {
        qWarning("Can't find barcode %s\n", barcode->toLatin1().data());
        return false;
    }

    ret = sqlite3_step(stmt);

    if (ret != SQLITE_ROW) {
        qWarning("Empty set parcel.\n");
        return false;
    }

    parcel->setExpressNum(QString((const char*)sqlite3_column_text(stmt, 0)));
    parcel->setLogisticsNum(QString((const char*)sqlite3_column_text(stmt, 1)));
    parcel->setBarcode(QString((const char*)sqlite3_column_text(stmt, 2)));
    parcel->setSender(QString((const char*)sqlite3_column_text(stmt, 3)));
    parcel->setRecipient(QString((const char*)sqlite3_column_text(stmt, 4)));
    parcel->setSendingAddr(QString((const char*)sqlite3_column_text(stmt, 5)));
    parcel->setRecivingAddr(QString((const char*)sqlite3_column_text(stmt, 6)));
    parcel->setCurrentAddr(QString((const char*)sqlite3_column_text(stmt, 7)));
    parcel->setNextAddr(QString((const char*)sqlite3_column_text(stmt, 8)));
    parcel->setIsOut(sqlite3_column_int(stmt, 9));
    sqlite3_finalize(stmt);

    return true;
}

bool ParcelDao::out(const QString* barcode)
{
    QString sql = QString("UPDATE parcel SET isOut='%1' WHERE barcode='%2'")
            .arg(1).arg(*barcode);

    char* errmsg = NULL;
    int ret = sqlite3_exec(db, sql.toLatin1().data(), NULL, NULL, &errmsg);

    if (ret == SQLITE_OK) {
        return true;
    } else {
        qWarning() << sqlite3_errmsg(db);
        return false;
    }
}


